package com.wei.health.controller;

import com.wei.health.model.dto.*;
import com.wei.health.model.vo.R;
import com.wei.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	// 登录
	@ResponseBody
	@PostMapping("/login")
	public R login(@Valid UserLoginDto userLoginDto) {
		return R.okAsAjax(userService.login(userLoginDto));
	}

	// 注册
	@ResponseBody
	@PostMapping("/register")
	public R register(@Valid UserRegisterDto userRegisterDto) {
		userService.register(userRegisterDto);
		return R.okAsAjax();
	}

	// 登出
	@GetMapping("/logout")
	public String logout(HttpServletRequest request) {
		userService.logout(request);
		return "redirect:/login?target=redirect";
	}

	// 修改密码
	@ResponseBody
	@PutMapping("/updatePassword")
	public R updatePassword(HttpServletRequest request, @RequestBody @Valid UserUpdatePasswordDto updatePasswordDto) {
		userService.updatePassword(request, getUserId(), updatePasswordDto);
		return R.okAsAjax();
	}

	// 修改用户信息
	@ResponseBody
	@PostMapping(value = "/updateUserInfo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public R updateUserInfo(@Valid UserUpdateInfoDto userUpdateInfoDto) {
		return R.okAsAjax(userService.updateUserInfo(getUserId(), userUpdateInfoDto));
	}

	// 用户管理
	@ResponseBody
	@GetMapping("/userList")
	public R userList(@Valid PageDto pageDto) {
		return R.okAsAjax(userService.userList(pageDto));
	}

	// 删除用户
	@ResponseBody
	@DeleteMapping("/{id}")
	public R saveBodyInfo(@PathVariable int id) {
		userService.deleteBodyInfo(id);
		return R.okAsAjax();
	}

	// 获取身体信息
	@ResponseBody
	@GetMapping("/{id}")
	public R getBodyInfo(@PathVariable int id) {
		return R.okAsAjax(userService.getBodyInfo(id));
	}


}